/*
 * AdminToolView.java
 */

package admintool;

import java.awt.TextArea;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import org.jdesktop.application.Action;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.SingleFrameApplication;

import admintool.imp03_data.IMP03_City;
import admintool.imp03_ui_components.MainSelectionListener;
import admintool.imp03_ui_components.SubSelectionListener;

/**
 * The application's main frame.
 */
public class AdminToolView extends FrameView {
    
    private static Connection dbConnection = null;
    /**
     * 
     */
    public static String EMPTY = "<empty>";
    /**
     * 
     */
    public static int ZERO = 0;
    
            
    /**
     * 
     * @return
     */
    public static Connection getDBConnection(){
        try {
            if(dbConnection == null){
	            String database = "planspiel_imp03";
	            String user = "root";
	            String password = "imp03"; 
	            String connectionUrl = "jdbc:mysql://localhost/" + database + "?user=" + user + "&password=" + password;
	            String driverName = "com.mysql.jdbc.Driver";

	            // falls properties vorhanden, dann auslesen
	            try {
	            	Properties props = new Properties();
	            	props.load(AdminToolView.class.getResourceAsStream("/admintool/resources/dbConnection.properties"));
	            	
	                database = props.getProperty("database");
	                user = props.getProperty("user");
	                password = props.getProperty("password"); 
	                connectionUrl = props.getProperty("connectionUrl");
	                driverName = props.getProperty("driverName");
	                
	                if ("com.google.cloud.sql.Driver".equals(driverName)) {
	                	AccessTokenRegistry.updateUserPrefs();
	                }
	            } catch (Exception e) {
					// ignore, use defaults
				}

                //Establish DB-Connection
                Class.forName(driverName);
                dbConnection = DriverManager.getConnection(connectionUrl);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JTextArea ta = new JTextArea(e.getMessage(), 10, 72);
            JOptionPane.showMessageDialog(null, ta, "ERROR: Datenbankverbindung", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
        
        return dbConnection;
    }
    
    private void inititalize_IMP03() {
        //Add a shutdown hook
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    if(dbConnection != null){
                        dbConnection.close();
                        System.err.println("> DB-CLOSED");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });    

        try {
            //initialize DB-Connection
            getDBConnection();

            //Add selection change listener to the ListBoxes
            list_mainSelection.addListSelectionListener(new MainSelectionListener(list_mainSelection, list_subSelection, panel_parameters, dbConnection));
            list_subSelection.addListSelectionListener(new SubSelectionListener(list_mainSelection, list_subSelection, panel_parameters, dbConnection));
            
            //initialize slow SQL-Statements
            IMP03_City.getAllAvailableCitiesFromDB(false);            
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showConfirmDialog(list_mainSelection, e.getMessage(), "DB Login", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }

    /**
     * 
     * @param app
     */
    public AdminToolView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        inititalize_IMP03();
    }

    /**
     * 
     */
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = AdminToolApp.getApplication().getMainFrame();
            aboutBox = new AdminToolAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        AdminToolApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list_subSelection = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        list_mainSelection = new javax.swing.JList();
        panel_parameters = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        list_subSelection.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list_subSelection.setName("list_subSelection"); // NOI18N
        jScrollPane1.setViewportView(list_subSelection);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        list_mainSelection.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Platform", "Company", "Skill", "Vacancy", "Candidate", "Message", "Tip" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        list_mainSelection.setName("list_mainSelection"); // NOI18N
        jScrollPane3.setViewportView(list_mainSelection);

        panel_parameters.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panel_parameters.setName("panel_parameters"); // NOI18N

        javax.swing.GroupLayout panel_parametersLayout = new javax.swing.GroupLayout(panel_parameters);
        panel_parameters.setLayout(panel_parametersLayout);
        panel_parametersLayout.setHorizontalGroup(
            panel_parametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 393, Short.MAX_VALUE)
        );
        panel_parametersLayout.setVerticalGroup(
            panel_parametersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 376, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_parameters, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
            .addComponent(panel_parameters, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(admintool.AdminToolApp.class).getContext().getResourceMap(AdminToolView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(admintool.AdminToolApp.class).getContext().getActionMap(AdminToolView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList list_mainSelection;
    private javax.swing.JList list_subSelection;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel panel_parameters;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;
}
